package com.example.abe.diandi.home.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;

import example.abe.com.framework.viewinject.ViewInjectUtils;

/**
 * Created by abe on 17/3/11.
 */

public class MainAddNoteView extends FrameLayout {

    @BindView(R.id.view_main_add_note_iv_start)
    protected ImageView mIvStart;

    @BindView(R.id.view_main_add_note_iv_edit_cancle)
    protected ImageView mIvCancle;

    @BindView(R.id.view_main_add_note_iv_edit_image)
    protected ImageView mIvImage;

    @BindView(R.id.view_main_add_note_iv_edit_text)
    protected ImageView mIvText;

    @BindView(R.id.view_main_add_note_fl_edit_bg)
    protected FrameLayout mFlEditBg;

    private Context mContext;

    private OnItemClickListener mListener;

    public MainAddNoteView(Context context) {
        super(context);
        init(context);
    }

    public MainAddNoteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MainAddNoteView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context){
        mContext = context;
        View root = LayoutInflater.from(mContext).inflate(R.layout.view_main_add_note, null);
        addView(root, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ViewInjectUtils.inject(this, root);
        mFlEditBg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //防止点击穿透
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public void setState(State state){
        switch (state){
            case OPEN:
                mFlEditBg.setVisibility(VISIBLE);
                mIvStart.setVisibility(GONE);
                break;
            case CLOSE:
                mFlEditBg.setVisibility(GONE);
                mIvStart.setVisibility(VISIBLE);
                break;
        }
    }

    @OnClick({R.id.view_main_add_note_iv_start, R.id.view_main_add_note_iv_edit_cancle, R.id.view_main_add_note_iv_edit_image, R.id.view_main_add_note_iv_edit_text})
    protected void onEditActionClick(View v){
        switch (v.getId()){
            case R.id.view_main_add_note_iv_start:
                setState(State.OPEN);
                break;
            case R.id.view_main_add_note_iv_edit_cancle:
                setState(State.CLOSE);
                break;
            case R.id.view_main_add_note_iv_edit_image:
                if (mListener != null){
                    mListener.onItemClick("图片");
                }
                break;
            case R.id.view_main_add_note_iv_edit_text:
                if (mListener != null){
                    mListener.onItemClick("文字");
                }
                break;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String text);
    }

    public enum State{
        OPEN,
        CLOSE,
    }
}
